package cn.test.learnkotlin.v1

private class JGeneric {

    class Box<T>(t: T) {
        var value = t
    }

    fun fooBox() {
        val box: Box<Int> = Box<Int>(1)
        val box1 = Box(1)
    }

    fun <T> doPrint(content: T) {
        when (content) {
            is Int -> println("is int:$content")
            is String -> println("is String:${content}")
            else -> println("other type:${content}")
        }
    }

    fun fooGenericMethod() {
        val age = 23
        val name = "runoob"
        val bool = true

        doPrint(age)
        doPrint(name)
        doPrint(bool)
    }

    /**
     * 形变
     */
    class Out<out A>(val a: A) {
        fun foo(): A {
            return a
        }
    }

    fun fooOut() {
        var str: Out<String> = Out("a")
        var any: Out<Any> = Out("any")
        //str = any//error
        any = str

    }

    /**
     * 形变
     */
    class In<in A>(a: A) {
        fun foo(a: A) {

        }
    }

    fun fooIn() {
        var str: In<String> = In("a")
        var any: In<Any> = In("any")
        str = any
    }

    fun main() {
        fooGenericMethod()
    }
}

fun main() {
    JGeneric().main()
}